<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <title>乳腺癌预测结果</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
  <style>
    body {
      background-color: #f8f9fa;
      font-family: 'Microsoft YaHei', Arial, sans-serif;
    }
    .result-container {
      margin-top: 30px;
      margin-bottom: 30px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      overflow: hidden;
    }
    .result-header {
      padding: 20px;
      color: white;
    }
    .loading-header {
      background: linear-gradient(135deg, #3498db, #2980b9);
    }
    .benign-header {
      background: linear-gradient(135deg, #56ab2f, #a8e063);
    }
    .malignant-header {
      background: linear-gradient(135deg, #cb2d3e, #ef473a);
    }
    .unrealistic-header {
      background: linear-gradient(135deg, #ffc107, #fd7e14);
    }
    .result-body {
      padding: 30px;
      background-color: white;
    }
    .confidence-bar {
      height: 30px;
      border-radius: 15px;
      margin: 20px 0;
    }
    .benign-bar {
      background: linear-gradient(to right, #56ab2f, #a8e063);
    }
    .malignant-bar {
      background: linear-gradient(to right, #cb2d3e, #ef473a);
    }
    .unrealistic-bar {
      background: linear-gradient(to right, #ffc107, #fd7e14);
    }
    .analysis-section {
      background-color: #f8f9fa;
      border-radius: 10px;
      padding: 20px;
      margin-top: 20px;
    }
    .time-section {
      margin-top: 20px;
      color: #6c757d;
      font-style: italic;
    }
    .result-fields {
      margin-bottom: 20px;
    }
    .result-field {
      margin-bottom: 15px;
      padding: 15px;
      background-color: #f8f9fa;
      border-radius: 5px;
      border-left: 4px solid #007bff;
    }
    .result-field h4 {
      margin-top: 0;
      color: #333;
      font-size: 1.1rem;
    }
    .result-text {
      margin-bottom: 0;
      line-height: 1.6;
      white-space: pre-wrap;
    }
    #loading-spinner {
      margin: 30px auto;
      text-align: center;
    }
    .spinner-border {
      width: 5rem;
      height: 5rem;
    }
    .hidden {
      display: none;
    }
    .visible {
      display: block;
    }
    .alert-section {
      margin-top: 20px;
    }
    .confidence-display {
      font-size: 1.2rem;
      font-weight: bold;
      margin: 10px 0;
    }
  </style>
</head>
<body>

<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-10">
      <div class="result-container">
        <!-- 结果标题 -->
        <div id="result-header" class="result-header loading-header">
          <h1 class="text-center">
            <i class="fas fa-spinner fa-spin"></i> AI 正在分析中...
          </h1>
        </div>
        
        <!-- 结果详情 -->
        <div class="result-body">
          <!-- 加载动画 -->
          <div id="loading-spinner">
            <div class="spinner-border text-primary" role="status">
              <span class="sr-only">加载中...</span>
            </div>
            <h4 class="mt-3">正在分析肿瘤数据，请稍候...</h4>
          </div>
          
          <!-- 错误提示 -->
          <div id="error-alert" class="alert alert-danger alert-section hidden" role="alert">
            <h4 class="alert-heading"><i class="fas fa-exclamation-triangle"></i> 请求错误</h4>
            <p id="error-message"></p>
            <div class="text-center mt-3">
              <a href="/" class="btn btn-primary">返回首页</a>
            </div>
          </div>
          
          <!-- 结果内容 - 初始隐藏 -->
          <div id="result-content" class="hidden">
            <!-- 三个输出字段的显示区域 -->
            <div class="result-fields">
                <div class="result-field">
                    <h4><i class="fas fa-diagnoses"></i> 判断结果:</h4>
                    <p id="judgment-result" class="result-text"></p>
                </div>
                <div class="result-field">
                    <h4><i class="fas fa-stethoscope"></i> 分析说明:</h4>
                    <p id="treatment-advice" class="result-text"></p>
                </div>
            </div>
            
            <div class="confidence-display text-center">
              分析置信度: <span id="accuracy-value">--</span>%
            </div>
            
            <!-- 置信度进度条 -->
            <div class="progress confidence-bar">
              <div id="confidence-bar" class="progress-bar" 
                   role="progressbar" 
                   style="width: 0%" 
                   aria-valuenow="0" 
                   aria-valuemin="0" 
                   aria-valuemax="100">
                <span id="confidence-text">0%</span>
              </div>
            </div>
            
            <div class="text-center mt-4">
              <a href="/" class="btn btn-primary btn-lg">
                <i class="fas fa-arrow-left"></i> 返回首页
              </a>
            </div>
          </div>
          
          <!-- 处理时间 -->
          <div class="time-section text-right">
            <p>处理时间: <span id="processing-time">0</span> 秒</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    // 获取表单数据
    const formData = {};
    
    // 定义我们需要的12个特征
    const features = [
        "radius_mean", "texture_mean", "perimeter_mean", "area_mean",
        "smoothness_mean", "compactness_mean", "concavity_mean",
        "concave_points_mean", "symmetry_mean", "fractal_dimension_mean",
        "radius_se", "texture_se"
    ];
    
    // 从URL参数中获取特征值
    const urlParams = new URLSearchParams(window.location.search);
    features.forEach(feature => {
        formData[feature] = urlParams.get(feature) || '0';
    });
    
    // 开始计时
    const startTime = Date.now();
    
    // 直接调用/predict接口获取结果
    fetch('/predict', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(formData)
    })
    .then(response => {
        if (!response.ok) {
            return response.json().then(err => { throw new Error(err.error || '服务器响应错误'); });
        }
        return response.json();
    })
    .then(data => {
        // 计算处理时间
        const processingTime = ((Date.now() - startTime) / 1000).toFixed(2);
        document.getElementById('processing-time').textContent = processingTime;
        
        // 隐藏加载动画
        document.getElementById('loading-spinner').style.display = 'none';
        
        // 更新标题和样式
        const resultHeader = document.getElementById('result-header');
        const confidenceBar = document.getElementById('confidence-bar');
        const confidenceText = document.getElementById('confidence-text');
        
        if (data.judgment_result.includes('恶性')) {
            resultHeader.className = 'result-header malignant-header';
            resultHeader.innerHTML = '<h1 class="text-center"><i class="fas fa-exclamation-triangle"></i> 预测结果：恶性肿瘤</h1>';
            confidenceBar.className = 'progress-bar malignant-bar';
        } else if (data.judgment_result.includes('良性')) {
            resultHeader.className = 'result-header benign-header';
            resultHeader.innerHTML = '<h1 class="text-center"><i class="fas fa-check-circle"></i> 预测结果：良性肿瘤</h1>';
            confidenceBar.className = 'progress-bar benign-bar';
        } else if (data.judgment_result.includes('不符合现实')) {
            resultHeader.className = 'result-header unrealistic-header';
            resultHeader.innerHTML = '<h1 class="text-center"><i class="fas fa-exclamation-circle"></i> 预测结果：数据不符合现实</h1>';
            confidenceBar.className = 'progress-bar unrealistic-bar';
        } else {
            resultHeader.className = 'result-header loading-header';
            resultHeader.innerHTML = `<h1 class="text-center">预测结果：${data.judgment_result}</h1>`;
            confidenceBar.className = 'progress-bar';
        }
        
        // 更新输出字段
        document.getElementById('judgment-result').textContent = data.judgment_result;
        document.getElementById('treatment-advice').textContent = data.treatment_advice;
        
        // 设置置信度
        document.getElementById('accuracy-value').textContent = data.confidence;
        confidenceBar.style.width = `${data.confidence}%`;
        confidenceBar.setAttribute('aria-valuenow', data.confidence);
        confidenceText.textContent = `${data.confidence}%`;
        
        // 显示结果内容
        document.getElementById('result-content').className = 'visible';
    })
    .catch(error => {
        console.error('请求错误:', error);
        
        // 隐藏加载动画
        document.getElementById('loading-spinner').style.display = 'none';
        
        // 显示错误信息
        const errorAlert = document.getElementById('error-alert');
        const errorMessage = document.getElementById('error-message');
        errorMessage.textContent = error.message;
        errorAlert.className = 'alert alert-danger alert-section visible';
    });
  });
</script>

</body>
</html>